package org.amphiprion.descentv2assistant.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.amphiprion.descentv2assistant.dao.xml.ConditionXmlDao;
import org.amphiprion.descentv2assistant.dao.xml.HeroXmlDao;

import android.database.Cursor;

public class EncounterHero extends Entity implements Comparable<EncounterHero> {
	private Hero hero;
	private String tag;
	private int health;
	private int stamina;
	private String gameId;
	private int xp;
	private int gold;
	private final List<Condition> conditions = new ArrayList<Condition>();

	public EncounterHero() {
		super(UUID.randomUUID().toString());
	}

	public EncounterHero(Cursor cursor) {
		id = cursor.getString(cursor.getColumnIndex("id"));
		tag = cursor.getString(cursor.getColumnIndex("tag"));
		health = cursor.getInt(cursor.getColumnIndex("health"));
		stamina = cursor.getInt(cursor.getColumnIndex("stamina"));
		gameId = cursor.getString(cursor.getColumnIndex("game_session_id"));
		xp = cursor.getInt(cursor.getColumnIndex("xp"));
		gold = cursor.getInt(cursor.getColumnIndex("gold"));

		String strCondition = cursor.getString(cursor.getColumnIndex("condition"));
		if (strCondition != null && !"".equals(strCondition)) {
			String[] strs = strCondition.split(";");
			for (int i = 0; i < strs.length; i++) {
				int pos = strs[i].indexOf("|");
				Condition c = ConditionXmlDao.getInstance().getCondition(strs[i].substring(0, pos)).clone();
				c.setCount(Integer.parseInt(strs[i].substring(pos + 1)));
				conditions.add(c);
			}
		}

		String heroId = cursor.getString(cursor.getColumnIndex("hero_id"));
		hero = HeroXmlDao.getInstance().getHero(heroId);
		if (health == -666) {
			health = hero.getCharacteristic().getHealth();
			stamina = hero.getCharacteristic().getStamina();
		}
	}

	public Hero getHero() {
		return hero;
	}

	public int getHealth() {
		return health;
	}

	public void setHealth(int health) {
		this.health = health;
	}

	public int getStamina() {
		return stamina;
	}

	public void setStamina(int stamina) {
		this.stamina = stamina;
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public List<Condition> getConditions() {
		return conditions;
	}

	public void setHero(Hero hero) {
		this.hero = hero;
	}

	public String getGameId() {
		return gameId;
	}

	public void setGameId(String gameId) {
		this.gameId = gameId;
	}

	public int getXp() {
		return xp;
	}

	public void setXp(int xp) {
		this.xp = xp;
	}

	public int getGold() {
		return gold;
	}

	public void setGold(int gold) {
		this.gold = gold;
	}

	@Override
	public int compareTo(EncounterHero another) {
		return hero.getName().compareTo(another.getHero().getName());
	}

	public String getConditionsForDB() {
		if (conditions.size() == 0) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		for (Condition c : conditions) {
			if (sb.length() > 0) {
				sb.append(";");
			}
			sb.append(c.getId());
			sb.append("|");
			sb.append(c.getCount());
		}
		return sb.toString();
	}

}
